package db;

import java.util.List;

import app.BearApplication;

/**
 * Created by dllo on 17/4/13.
 */
//用单例模式写一个数据库工具
public class DatabaseTool implements TableCarOperation, TableRecordOperation {

    @Override
    public void addRecord(RecordEntity record) {
        mRecordOperation.addRecord(record);
    }

    @Override
    public void removeRecord(int id) {
        mRecordOperation.removeRecord(id);
    }

    @Override
    public void updateRecord(RecordEntity car) {
        mRecordOperation.updateRecord(car);
    }

    @Override
    public List<RecordEntity> queryRecord() {
        return mRecordOperation.queryRecord();
    }

    @Override
    public List<RecordEntity> querySelectedYearRecord() {
        return mRecordOperation.querySelectedYearRecord();
    }

    @Override
    public List<RecordEntity> querySelectedHalfYearRecord() {
        return mRecordOperation.querySelectedHalfYearRecord();
    }

    @Override
    public List<RecordEntity> querySelectedThreeMonthsRecord() {
        return mRecordOperation.querySelectedThreeMonthsRecord();
    }

    @Override
    public List<MoneyEntity> queryEachYearMoney() {
        return mRecordOperation.queryEachYearMoney();
    }

    @Override
    public List<MoneyEntity> queryEachMonthMoney() {
        return mRecordOperation.queryEachMonthMoney();
    }

    //静态内部类的形式 创建一个单例对象
    private static class SingletonHolder {
        private static final DatabaseTool INSTANCE = new DatabaseTool();
    }

    public static DatabaseTool getInstance() {
        return SingletonHolder.INSTANCE;
    }

    //全局的变量能私有的话就私有 变量的作用域能是局部的就不要写成全局的
    private BearSQLiteHelper mHelper;
    private TableCarOperation mCarOperation;
    private TableRecordOperation mRecordOperation;

    //私有化构造方法
    private DatabaseTool() {
        mHelper = new BearSQLiteHelper(BearApplication.getContext());
        mCarOperation = new CarOperationAndroid(mHelper);
        mRecordOperation = new RecordOperationAndroid(mHelper);
    }

    //插入方法
    public void addCar(CarEntity car) {
        mCarOperation.addCar(car);
    }

    //删除方法
    public void removeCar(int id) {
        mCarOperation.removeCar(id);
    }

    //更新方法
    public void updateCar(CarEntity car) {
        mCarOperation.updateCar(car);
    }

    //查询方法
    public List<CarEntity> queryCars() {
        return mCarOperation.queryCars();
    }

    public CarEntity querySelectedCar() {
        return mCarOperation.querySelectedCar();
    }

    //更改选中的车辆
    public void changeSelectedCar(int id) {
        mCarOperation.changeSelectedCar(id);
    }

    public void changeSelectedCar(CarEntity newSelectedCar) {
        mCarOperation.changeSelectedCar(newSelectedCar);
    }


}
